﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class ModeSetCenter : System.Web.UI.Page
{
    MyDbHelper mydb = new MyDbHelper();
    DbHelper db = new DbHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            IniModleTree();
        }
    }
    protected void MyData_Refresh(object sender, StoreReadDataEventArgs e)
    {
        
    }
    protected void Btn_SaveForm(object sender, DirectEventArgs e)
    {
        //保存
        string sql = "";
        if (hf_State.Value.ToString() == "Add")
        {
            sql = @"insert into sys_modules(parentid,mindx,mtype,modulecode,modulename,moduletype,moduletab,weburl,creater,block)
            values(" + this.hf_parent_id.Value + ","
                   + this.txtindx.Text + ","
                   + this.txtstype.Value + ",'"
                   + this.txtmodecode.Text + "','"
                   + this.txtmodename.Text + "',"
                   + this.txtmtype.Value + ",'"
                   + this.txttablename.Text + "','"
                   + CheckString(this.txturl.Text) + "','"
                   + Session["UserCode"] + "',1"
                   + ")";
        }
        else
        {
            sql = "update sys_modules set mindx=" + this.txtindx.Text + ",modulecode='" + this.txtmodecode.Text + "',mtype=" + this.txtstype.Value
                + ",modulename='" + this.txtmodename.Text + "',moduletype=" + this.txtmtype.Value + ",moduletab='"+txttablename.Text+"',modifydate='" + DateTime.Now.ToString() + "'"
                + ",weburl='" + CheckString(this.txturl.Text) + "'" + ",creater='" + Session["UserCode"] + "',block=1 where mid=" + this.hf_tree_id.Value;
        }
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            X.Msg.Alert("提示", "保存成功！").Show();
            IniModleTree();
        }
    }
    protected void DoAdd(object sender, DirectEventArgs e)
    {
        //同级
        this.hf_State.Value = "Add";
        DataSet ds = mydb.DM_GetDateSet("select modulename from sys_modules where mid =" + hf_parent_id.Value);
        string name="";
        if (ds.Tables[0].Rows.Count > 0)
        {
            name = ds.Tables[0].Rows[0][0].ToString();

        }
        else
        {
            hf_parent_id.Value = "0";
        }
        AddModes(name);
    }
    protected void DoDwonAdd(object sender, DirectEventArgs e)
    {
        //下级
        this.hf_State.Value = "Add";
        string name = mydb.DM_GetExecuteScalarObj("select modulename from sys_modules where mid =" + this.hf_tree_id.Value).ToString();
        this.hf_parent_id.Value = hf_tree_id.Value;
        AddModes(name);
    }
    protected void AddModes(string parent)
    {
        this.txtindx.Text = "";
        this.txtmodecode.Text = "";
        this.txtmodename.Text = "";
        this.txtmtype.Value = "";
        this.txturl.Text = "";
        this.txttablename.Text = "";
        this.txtstype.Value = "";
        this.txtparent.Text = parent;
    }
    protected void BtnDelete(object sender, DirectEventArgs e)
    {
        if (mydb.DM_ExecuteNonQuery("delete from sys_modules where mid=" + hf_tree_id.Value) != -1)
        {
            X.Msg.Alert("提示", "删除成功！").Show();
            IniModleTree();
        }
        else
        {
            X.Msg.Alert("提示", "删除失败！").Show();
            IniModleTree();
        }
    }
    public void IniModleTree()
    {
        this.TreePanel1.Reload();
        this.TreePanel1.Refresh();
        string sql = "select * from sys_modules where parentid = 0 order by mindx";
        DataSet ds = mydb.DM_GetDateSet(sql);
        Node mNode = new Node();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Node node = new Node();
                node.NodeID = dr["mid"].ToString();
                node.Text = "[" + dr["mindx"].ToString() + "]" + dr["modulename"].ToString();
                node.Qtip = dr["modulename"].ToString();
                node.IconCls = "Folder";
                mNode.Children.Add(node);
                AddChildNode(node, dr["mid"].ToString());
            }
            this.TreePanel1.Root.Add(mNode);
            //this.TreePanel1.CollapseAll();
        }
    }
    private void AddChildNode(Node node, string parentid)
    {
        string sql = "select * from sys_modules where parentid = " + parentid + " order by mindx";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Node childNode = new Node();
                childNode.NodeID = dr["mid"].ToString();
                childNode.Text = "[" + dr["mindx"].ToString() + "]" + dr["modulename"].ToString();
                childNode.Qtip = dr["modulename"].ToString();
                node.Children.Add(childNode);
                AddChildNode(childNode, dr["mid"].ToString());
            }
        }
    }
    protected void Tree_Row_Click(object sender, DirectEventArgs e)
    {
        TreeSelectionModel sm = this.TreePanel1.GetSelectionModel() as TreeSelectionModel;
        this.hf_tree_id.Value = sm.SelectedRecordID;
        //X.Msg.Alert("Submit", sm.SelectedRecordID).Show();
        DataSet ds = mydb.DM_GetDateSet(@"select b.modulename as parentname,a.* from sys_modules a left 
                        join sys_modules b on b.mid = a.parentid  where a.mid=" + sm.SelectedRecordID);
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr = ds.Tables[0].Rows[0];
            this.txtparent.Text = dr["parentname"].ToString();
            txtmodecode.Text = dr["modulecode"].ToString();
            txtmodename.Text = dr["modulename"].ToString();
            txtindx.Text = dr["mindx"].ToString();
            txturl.Text = dr["weburl"].ToString();
            txtmtype.Value = dr["moduletype"].ToString();
            txttablename.Text = dr["moduletab"].ToString();
            txtstype.Value = dr["mtype"].ToString();
            this.hf_parent_id.Value = dr["parentid"].ToString();
        }
        
        bindldyout();
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void Edit2(int fid, string field, string oldValue, string newValue, object customer)
    {
        if (field == "fieldvisible" || field == "fieldreadonly" || field == "isctrl")
        {
            if (mydb.DM_ExecuteNonQuery("update sys_moduleslayout set " + field + "=" + (newValue=="true"?"1":"0") + " where LID=" + fid) != -1)
            {
                this.GridPanel3.GetStore().GetById(fid).Commit();
            }
            else
            {
                X.Msg.Notify(new NotificationConfig()
                {
                    Title = "消息提示",
                    Html = "修改失败！",
                    Width = 250
                }).Show();
            }
        }
        else
        {
            if (mydb.DM_ExecuteNonQuery("update sys_moduleslayout set " + field + "='" + newValue + "' where LID=" + fid) != -1)
            {
                this.GridPanel3.GetStore().GetById(fid).Commit();
                if (field == "fielddisplay")
                {
                    LayoutInfo lt = JSON.Deserialize<LayoutInfo>(customer.ToString());
                    mydb.DM_ExecuteNonQuery("Exc_FieldDisPlay '" + lt.fieldname + "','" + lt.fielddisplay + "'");
                }

            }
            else
            {
                X.Msg.Notify(new NotificationConfig()
                {
                    Title = "消息提示",
                    Html = "修改失败！",
                    Width = 250
                }).Show();
            }
        }
       
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        //创建风格布局
        if (this.txtmtype.Value.ToString() == "0" || this.txtmtype.Value.ToString() == "1")
        {
            X.Msg.Alert("提示", "当前类型无法创建布局内容！").Show();
            return;
        }
        DataTable dt;

        if (this.txtstype.Value.ToString() == "1")
            dt = db.DM_GetDateSet(this.txturl.Text).Tables[0];
        else
            dt = mydb.DM_GetDateSet(this.txturl.Text).Tables[0];

        string sql = "";
        string caption = "";
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            caption = mydb.DM_GetExecuteScalarObj("Exc_FieldDisPlay '" + dt.Columns[i].ColumnName + "',''").ToString();
            sql += @"if not exists(select * from sys_moduleslayout where mid = " + this.hf_tree_id.Value + @" and fieldname='" + dt.Columns[i].ColumnName + @"') 
                    insert into sys_moduleslayout(mid,lindx,fieldname,fieldwidth,fielddisplay,fieldvisible,gridfoot,datatype)
                    values(" + this.hf_tree_id.Value + "," + i.ToString() + ",'" + dt.Columns[i].ColumnName + "',80,'" + caption + "',1,0,'" + dt.Columns[i].DataType.Name.ToString().ToLower() + "')";
        }
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            X.Msg.Alert("提示", "加载成功！").Show();
        }
        else
        {
            X.Msg.Alert("提示", "加载失败！").Show();
        }
        bindldyout();
    }
    protected void BatchDel_Click(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;

        foreach (SelectedRow row in sm.SelectedRows)
        {
            mydb.DM_ExecuteNonQuery("delete from sys_moduleslayout where lid=" + row.RecordID);
        }
        bindldyout();
    }

    private void bindldyout()
    {
        //绑定全局布局
        string sql = @"select lid,mid,lindx,fieldname,fielddisplay,fieldwidth,fieldvisible,gridfoot,fieldreadonly,isctrl,
        datatype,showtype,ctrltype,ctrl_x,ctrl_y,iskeyid,ismust
        from sys_moduleslayout where mid =" + this.hf_tree_id.Value;
        this.Store3.DataSource = mydb.DM_GetDateSet(sql);
        this.Store3.DataBind();
    }
    public string CheckString(string str)
    {
        string returnStr = "";
        if (str.IndexOf("'") != -1) //判断字符串是否含有单引号
        {
            returnStr = str.Replace("'", "''");
            str = returnStr;
        }
        return str;
    }

}

public class LayoutInfo
{
    public string lid { get; set; }
    public string fieldname { get; set; }
    public string fielddisplay { get; set; }
    public string fieldwidth { get; set; }
    public string fieldvisible { get; set; }
    public string gridfoot { get; set; }
    public string datatype { get; set; }
}